use strict;
use warnings;
use XML::Bare;

# Load xml file
my $xml = new XML::Bare( file => "Reference.xml")
    or die "Can't read Reference.xml file";
my $reference = $xml->parse()
    or die "Invalid xml file";

print "Read article information from Reference.xml file";
my @articles = @{$reference->{xml}->{Article}};

# Format
my $format = {
    Journal=>"[#ID#] #Author# #Title#[J]. #Publication#, #Year#, #Volume#: #StartPage#-#EndPage#.",
    Monograph=>"[#ID#] #Author#. #Title#[M]. #Location#: #Press#, #Year#.",
    Conference=>"[#ID#] #Author# #Title#[A]. In #Publication#[C] #Year#: #StartPage#-#EndPage#.",
    Report=>"[#ID#] #Author# #Title#[R]. #Location#: #Institution#, #Year#:",
};
my $authorSeperator = ", ";

my $outputFile = "Reference.txt";
open FILE, ">$outputFile"
    or die "Can't create $outputFile file";

# Output to Reference.output file
print "Begin to output $outputFile file\n";
foreach  my $id (0 .. scalar(@articles) - 1) {
    my $seqId = $id + 1;
    my $line = $format->{$articles[$id]->{Type}->{value}};
    if(not defined $line) {
        print "Invalid Type for $articles[$id]->{Title}->{value}. Abort...\n";
        return 1;
    }

    $line =~ s/#ID#/$seqId/;

    die "No title information" if not defined $articles[$id]->{Title}->{value};
    $line =~ s/#Title#/$articles[$id]->{Title}->{value}/;
    die "No year information" if not defined $articles[$id]->{Year}->{value};
    $line =~ s/#Year#/$articles[$id]->{Year}->{value}/;
    
    # Journal
    if($articles[$id]->{Type}->{value} eq "Journal") {
        die "No publication information" if not defined $articles[$id]->{Publication}->{value};
        $line =~ s/#Publication#/$articles[$id]->{Publication}->{value}/;

        die "No volume information" if not defined $articles[$id]->{Volume}->{value};
        $line =~ s/#Volume#/$articles[$id]->{Volume}->{value}/;
        
        die "No StartPage information" if not defined $articles[$id]->{StartPage}->{value};
        $line =~ s/#StartPage#/$articles[$id]->{StartPage}->{value}/;

        die "No EndPage information" if not defined $articles[$id]->{EndPage}->{value};
        $line =~ s/#EndPage#/$articles[$id]->{EndPage}->{value}/;
    }
    elsif($articles[$id]->{Type}->{value} eq "Monograph"){
        die "No Location information" if not defined $articles[$id]->{Location}->{value};
        $line =~ s/#Location#/$articles[$id]->{Location}->{value}/;

        die "No Press information" if not defined $articles[$id]->{Press}->{value};
        $line =~ s/#Press#/$articles[$id]->{Press}->{value}/;
    }
    elsif($articles[$id]->{Type}->{value} eq "Conference"){
        die "No publication information" if not defined $articles[$id]->{Publication}->{value};
        $line =~ s/#Publication#/$articles[$id]->{Publication}->{value}/;

        die "No StartPage information" if not defined $articles[$id]->{StartPage}->{value};
        $line =~ s/#StartPage#/$articles[$id]->{StartPage}->{value}/;

        die "No EndPage information" if not defined $articles[$id]->{EndPage}->{value};
        $line =~ s/#EndPage#/$articles[$id]->{EndPage}->{value}/;
    }
    elsif($articles[$id]->{Type}->{value} eq "Conference"){
        die "No Location information" if not defined $articles[$id]->{Location}->{value};
        $line =~ s/#Location#/$articles[$id]->{Location}->{value}/;

        die "No Institution information" if not defined $articles[$id]->{Institution}->{value};
        $line =~ s/#Institution#/$articles[$id]->{Institution}->{value}/;
    }

    my $authorList = '';
    if(ref($articles[$id]->{Author}) eq 'ARRAY') {
        my $isPrintEtAl = scalar(@{$articles[$id]->{Author}}) > 3 ? 1 : 0;
        my $lastAuthorId = scalar(@{$articles[$id]->{Author}}) >= 3 ? 2 : 1;
        foreach ( 0 .. $lastAuthorId - 1) {
            $authorList .= $articles[$id]->{Author}->[$_]->{name}->{value} . $authorSeperator;
        }
        $authorList .= $articles[$id]->{Author}->[$lastAuthorId]->{name}->{value};
        $authorList .= $authorSeperator . "et al." if $isPrintEtAl;
    }
    else {
        $authorList = $articles[$id]->{Author}->{name}->{value};
    }
    $line =~ s/#Author#/$authorList/;
    print "[$seqId] $articles[$id]->{Title}->{value}\n";
    print FILE $line . "\n";
}

close FILE;
